﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using Convertor.Interface;
using Wpf.CommonTools;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.Encrypt
{
    [Flags]
    public enum EncryptTypes
    {
        Md5 = 1,
        Symmetric = 2,
        SHA1 = 4,
        SHA256 = 8,
        SHA384 = 16,
        SHA512 = 32
    }

    public enum DirectionTypes
    {
        Encrypt,
        Decrypt
    }

    [Serializable]
    public class ConfigContent : IConfigContent, ISerializable, ICloneable, INotifyPropertyChanged
    {
        private EncryptTypes encriptType = EncryptTypes.Md5;
        /// <summary>
        /// 
        /// </summary>
        public EncryptTypes EncryptType
        {
            get { return encriptType; }
            set
            {
                encriptType = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("EncryptType"));
                    PropertyChanged(this, new PropertyChangedEventArgs("SymmetricType"));
                    PropertyChanged(this, new PropertyChangedEventArgs("ToolTip"));
                }
            }
        }
        private DirectionTypes symmetricType = DirectionTypes.Encrypt;
        /// <summary>
        /// 
        /// </summary>
        public DirectionTypes SymmetricType
        {
            get { return symmetricType; }
            set { symmetricType = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private string key = "jcsmkjGuz(%&hj7x89H$y";
        /// <summary>
        /// 
        /// </summary>
        public string Key
        {
            get { return key; }
            set { key = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private EncodingContainer encodingContent = new EncodingContainer() { DisplayName = UILanguage.FindResource("Plugin_Encrypt_DefaultEncoder"), Encoder = Encoding.Default };
        /// <summary>
        /// 
        /// </summary>
        public EncodingContainer EncodingContent
        {
            get { return encodingContent; }
            set { encodingContent = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }

        public override string ToString()
        {
            return ToolTip;
        }

        public ConfigContent()
        {
        }

        #region IToolTip 成员

        public string ToolTip
        {
            get
            {
                string a;
                switch (encriptType)
                {
                    case EncryptTypes.Symmetric:
                        a = string.Format(UILanguage.TryFindResource("Plugin_Encrypt_Info_Symmetric"),
                            EncryptTypes.Symmetric,
                            UILanguage.TryFindResource("Plugin_Encrypt_" + this.symmetricType),
                            this.encodingContent.DisplayName,
                            this.key);
                        break;
                    default:
                        a = string.Format(UILanguage.TryFindResource("Plugin_Encrypt_Info_Other"), encriptType, this.encodingContent.DisplayName);
                        break;
                }
                return a;
            }
        }

        #endregion

        #region ISerializable 成员

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("EncryptType", EncryptType, typeof(EncryptTypes));
            info.AddValue("EncodingContent", EncodingContent, typeof(EncodingContainer));
            info.AddValue("SymmetricType", SymmetricType, typeof(DirectionTypes));
            info.AddValue("Key", Key, typeof(string));
        }

        public ConfigContent(SerializationInfo info, StreamingContext context)
        {
            encriptType = (EncryptTypes)info.GetValue("EncryptType", typeof(EncryptTypes));
            encodingContent = (EncodingContainer)info.GetValue("EncodingContent", typeof(EncodingContainer));
            symmetricType = (DirectionTypes)info.GetValue("SymmetricType", typeof(DirectionTypes));
            Key = (string)info.GetValue("Key", typeof(string));
        }

        #endregion

        #region ICloneable 成员

        public object Clone()
        {
            ConfigContent content = new ConfigContent()
            {
                EncryptType = this.EncryptType,
                EncodingContent = this.EncodingContent,
                SymmetricType = this.SymmetricType,
                Key = this.key
            };
            return content;
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }
}
